var Spaceship=function(){
    this.imgSpaceship = new Image(); 
    this.x = 572.5;
    this.y = 540;
    this.imgWidth;
    this.bullet;
    this.isOver = false;
    
    var self=this;
    this.loadImage=function (){
        this.imgSpaceship.onload = function() {
            context.drawImage(self.imgSpaceship,self.x, self.y, 100, 70 );
            self.imgWidth =  this.width;
            
        };
        self.imgSpaceship.src = 'Images/spaceship.png';
    }
    
    this.moveleft = function(){
        if(this.x>0)
            this.x -= 20;
    }

    this.moveright = function(){
        if(this.x <= canvas.width-100)
            this.x += 20;
    }
    
    this.checkKey = function(event){
        if(!this.isOver){
            if (!event){
                event = window.event;
            }
            var code = event.keyCode;
            if (event.charCode && code == 0){
                code = event.charCode;
            }  
            switch(code) {
                case 37:
                    this.moveleft();
                    this.render();
                    break;
                case 39:
                    this.moveright();
                    this.render();
                    break;
                case 32:
                    if(this.bullet==null){
                        var shootSound = document.getElementById('shootSound');
                        shootSound.play();
                        this.bullet=new Bullet(this.x+this.imgWidth , this.y);
                    }
                    break;
            }     
        }
    }
    this.render = function()
    {
        context.clearRect(0,this.y,canvas.width,100);    
        this.loadImage();
    }
}
